package com.example.smart.adapter.home

import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.lib.extend.autoInt
import com.example.lib.extend.loadCircleImage
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.ToastUtils
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.databinding.ItemMySheepBinding
import com.example.smart.room.bean.AnimalBean
import java.util.HashMap


class MySheepAdapter(context: Context) : BaseRecyclerAdapter<ItemMySheepBinding, AnimalBean>(context) {

    private val mMap = HashMap<Int, Boolean>()

    init {
        mMap[0] = true
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemMySheepBinding> {
        return BaseViewHolder(ItemMySheepBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_my_sheep, parent, false)))
    }

    override fun onBindView(itemView: ItemMySheepBinding, bean: AnimalBean, position: Int) {
        itemView.ItemImage.loadCircleImage(bean.img, error = R.mipmap.icon_after_scan)
        itemView.ItemTitle.text = "${bean.type}羊${bean.earNo}号"
        itemView.ItemSex.text = "${bean.sex}羊"
        itemView.ItemTemplate.text = "平均体温38°C"
        if (mMap[position] == null || !mMap[position]!!) {
            itemView.ItemSexCount.text = "未配种"
            itemView.ItemSexCountImage.setImageResource(R.mipmap.icon_sheep_close)
        } else {
            itemView.ItemSexCount.text = "已配种"
            itemView.ItemSexCountImage.setImageResource(R.mipmap.icon_already_sex)
        }

        itemView.ItemSexCountImage.setOnDoubleClickListener {
            if (mMap[position] == null || !mMap[position]!!) {
                itemView.ItemSexCount.text = "已配种"
                itemView.ItemSexCountImage.setImageResource(R.mipmap.icon_already_sex)
                mMap[position] = true
                ToastUtils.showToast("牲畜已配种")
            } else {
                itemView.ItemSexCount.text = "未配种"
                itemView.ItemSexCountImage.setImageResource(R.mipmap.icon_sheep_close)
                mMap[position] = false
                ToastUtils.showToast("牲畜未配种")
            }
        }

        itemView.root.setOnDoubleClickListener {
            clickListener?.click(bean, position)
        }

        itemView.root.setOnLongClickListener {
            clickListener?.other(bean, position)
            return@setOnLongClickListener true
        }
    }

}